/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.digitalization.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.LogConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.digitalization.entity.ManageRsdawjEntity;
import org.springblade.modules.digitalization.manager.LogManager;
import org.springblade.modules.digitalization.service.IManageRsdamlService;
import org.springblade.modules.digitalization.service.IManageRsdawjService;
import org.springblade.modules.digitalization.vo.ManageRsdawjVO;
import org.springblade.modules.digitalization.wrapper.ManageRsdawjWrapper;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 档案存储页信息表-JAVA 控制器
 *
 * @author RS
 * @since 2023-01-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-rsdawj/rsdawj")
@Api(value = "档案存储页信息表-JAVA", tags = "档案存储页信息表-JAVA接口")
public class ManageRsdawjController extends BladeController {

	private final IManageRsdawjService rsdawjService;

	private final IManageRsdamlService rsdamlService;

	private final LogManager logManager;

	/**
	 * 档案存储页信息表-JAVA 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入rsdawj")
	public R<ManageRsdawjVO> detail(ManageRsdawjEntity rsdawj) {
		ManageRsdawjEntity detail = rsdawjService.getOne(Condition.getQueryWrapper(rsdawj));
		return R.data(ManageRsdawjWrapper.build().entityVO(detail));
	}

	/**
	 * 档案存储页信息表-JAVA 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入rsdawj")
	public R<IPage<ManageRsdawjVO>> list(ManageRsdawjEntity rsdawj, Query query) {
		IPage<ManageRsdawjEntity> pages = rsdawjService.page(Condition.getPage(query), Condition.getQueryWrapper(rsdawj).orderByAsc("sort").orderByAsc("create_time"));
		return R.data(ManageRsdawjWrapper.build().pageVO(pages));
	}


	@GetMapping("/listAllWj")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入rsdawj")
	public R<List<ManageRsdawjVO>> list(String a0000) {
		List<ManageRsdawjVO> pages = rsdawjService.getWjByA000(a0000);
		return R.data(pages);
	}

	/**
	 * 档案存储页信息表-JAVA 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入rsdawj")
	public R<IPage<ManageRsdawjVO>> page(ManageRsdawjVO rsdawj, Query query) {
		IPage<ManageRsdawjVO> pages = rsdawjService.selectManageRsdawjPage(Condition.getPage(query), rsdawj);
		return R.data(pages);
	}


	/**
	 * 档案存储页信息表-JAVA 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入rsdawj")
	public R update(@Valid @RequestBody ManageRsdawjEntity rsdawj) {
		return R.status(rsdawjService.updateById(rsdawj));
	}

	/**
	 * 档案存储页信息表-JAVA 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入rsdawj")
	public R submit(@Valid @RequestBody ManageRsdawjEntity newData) {


		ManageRsdawjEntity oldData = rsdawjService.getById(newData.getId());

		boolean isUpdate = newData.getId() != null;

		boolean b = rsdawjService.saveOrUpdate(newData);
		if (isUpdate) {
			logManager.doUpdate(newData.getA0000(), LogManager.TYPE_WJ, oldData, newData);
		} else {
			logManager.doAdd(newData.getA0000(), LogManager.TYPE_WJ, newData);
		}
		R<Object> da = R.status(b);
		return da.data(newData);
	}

	@PostMapping("/submit/log")
	public void submitLog(@Valid @RequestBody ManageRsdawjEntity manageRsdawjEntity) {
		// 只更新一下时间，记录日志
		ManageRsdawjEntity newData = rsdawjService.getById(manageRsdawjEntity.getId());
		ManageRsdawjEntity oldData = ObjectUtil.clone(newData);
		rsdawjService.saveOrUpdate(newData);
		logManager.doUpdate(oldData.getA0000(), LogManager.TYPE_WJ, oldData, newData);
	}

	/**
	 * 档案存储页信息表-JAVA 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<ManageRsdawjEntity> rsdawjEntities = rsdawjService.listByIds(Func.toLongList(ids));
		boolean b = rsdawjService.deleteLogic(Func.toLongList(ids));
		rsdawjEntities.stream().forEach(item -> {
			logManager.doDel(item.getA0000(), LogManager.TYPE_WJ, item);
		});
		return R.status(b);
	}

	/**
	 * 档案存储页信息表-JAVA 删除
	 */
	@PostMapping("/updatePage")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "更新排序", notes = "传入ids")
	@Deprecated
	public R updatePages(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(rsdawjService.updateSorts(Func.toLongList(ids)));
	}

	/**
	 * 获取指定路径下的图片
	 *
	 * @param request   响应请求
	 * @param type      类型hd ,org
	 * @param RSDAWJ000 目录ID
	 * @param RSDAWJ001 也许好
	 * @return 图片资源
	 * @throws IOException 文件读取异常
	 */
	@GetMapping(value = "/images/{type}/{RSDAWJ000}/{RSDAWJ001}")
	public ResponseEntity<Resource> getImage(HttpServletRequest request, @PathVariable String type, @PathVariable String RSDAWJ000, @PathVariable String RSDAWJ001) throws IOException {
		ResponseEntity<Resource> result = rsdawjService.getImage(request, type, RSDAWJ000, RSDAWJ001);
		return result;
	}
}
